﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace TBlog.Models
{
    public class TBlogService : ITBlogService
    {
        //
        private ITBlogRepository _repository;

        //
        public TBlogService()
        {
            _repository = new TBlogRepository();
        }

        //
        public List<Category> ListCategory()
        {
            return _repository.ListCategory().ToList();
        }

        public bool CreateCategory(Category categoryToCreate)
        {
            try
            {
                _repository.CreateCategory(categoryToCreate);
            }
            catch
            {
                return false;
            }
            return true;
        }

        public Category GetCategory(int? categoryId)
        {
            if (categoryId.HasValue)
                return _repository.GetCategory(categoryId);
            return _repository.GetFirstCategory();
        }

        public bool DeleteCategory(Category categoryToDelete)
        {
            try
            {
                _repository.DeleteCategory(categoryToDelete);
            }
            catch
            {
                return false;
            }
            return true;
        }

        public Category GetFirstCategory()
        {
            return _repository.GetFirstCategory();
        }

        public List<Post> ListPost()
        {
            return _repository.ListPost().ToList();
        }

        public List<Post> ListPost(int categoryId)
        {
            return _repository.ListPost(categoryId).ToList();
        }

        public bool CreatePost(Post postToCreate)
        {
            try
            {
                _repository.CreatePost(postToCreate);
            }
            catch
            {
                return false;
            }
            return true;
        }

        public bool DeletePost(Post postToDelete)
        {

            try
            {
                _repository.DeletePost(postToDelete);
            }
            catch
            {
                return false;
            }
            return true;
        }

        public Post GetPost(int? postId)
        {
            return _repository.GetPost(postId);
        }

        public Post GetFirstPost()
        {
            return _repository.GetFirstPost().FirstOrDefault();
        }

        public bool EditCategory(Category categoryToEdit)
        {
            try
            {
                _repository.EditCategory(categoryToEdit);
            }
            catch
            {
                return false;
            }
            return true;
        }

        public bool EditPost(Post postToEdit)
        {
            try
            {
                _repository.EditPost(postToEdit);
            }
            catch
            {
                return false;
            }
            return true;
        }

        public List<Post> ListPost(string postType)
        {
            return _repository.ListPost(postType).ToList();
        }
    }
}